home *** CD-ROM | disk | FTP | other *** search
- #include <io.h>
- #include <string.h>
- #include <stdio.h>
- #include <assert.h>
-
- void xstdio();
-
- int main()
- {
- xstdio();
- }
-
- void xstdio()
- {
- FILE *fp, tempin, tempout;
- char buf[100];
-
- printf("==> starting xstdio <==\n");
-
- // --- test fgetc on an empty file
- fp = fopen("testdata.txt","w+"); // --- open for r/w and truncate
-
- assert(fp!=NULL && fp>stdprn);
- assert(ferror(fp)==0 && feof(fp)==0);
- assert(fgetc(fp)==EOF);
-
- assert(feof(fp)!=0 && ferror(fp)==0);
- clearerr(fp);
- assert(feof(fp)==0 && ferror(fp)==0);
-
- // --- now we write some bytes to the file
- assert(fwrite("bigboy",3,2,fp)==2); // --- write 2 elements, each of size 3 bytes
- assert(fputs("badboy",fp)!=EOF); // --- and another 6 bytes
- assert(fputc('x',fp)=='x'); assert(putc('y',fp)=='y');
- assert(fputc('\n',fp)=='\n'); assert(putc('z',fp)=='z'); // --- for translated file '\n' expands to '\r\n'
- assert(ftell(fp)==17L);
-
- assert(fflush(fp)==0);
- assert(fseek(fp,0L,SEEK_SET)==0); // --- rewind to the start of file
- assert(ftell(fp)==0L);
- assert(fgets(buf,100,fp)==buf);
- assert(strcmp(buf,"bigboybadboyxy\n")==0);
- assert(fclose(fp)==0);
-
- // --- now we read some bytes from the file
- fp = fopen("testdata.txt","r");
- assert(fgetc(fp)=='b'); assert(fgetc(fp)=='i');
- assert(getc(fp)=='g'); assert(getc(fp)=='b');
- assert(ungetc('m',fp)=='m'); assert(ungetc('n',fp)=='n');
- assert(fgets(buf,100,fp)==buf);
- assert(strcmp(buf,"nmoybadboyxy\n")==0);
-
- assert(ftell(fp)==16L);
- assert(fgets(buf,100,fp)==buf);
- assert(strcmp(buf,"z")==0);
- assert(feof(fp)!=0 && ferror(fp)==0);
- assert(fclose(fp)==0);
-
- // --- now we reassign stdin/stdout and issue standard i/o calls
- memcpy(&tempin,stdin,sizeof(FILE));
- fp=fopen("testdata.txt","r");
- assert(fp!=NULL && fp>stdprn);
- memcpy(stdin,fp,sizeof(FILE));
-
- assert(fread(buf,3,2,stdin)==2); // --- read in two elements (words) each of size 3 characters
- assert(strncmp(buf,"bigboy",3*2)==0);
-
- assert(getchar()=='b'); assert(getchar()=='a');
- assert(gets(buf)==buf);
- assert(strcmp(buf,"dboyxy")==0);
- assert(gets(buf)==buf);
- assert(strcmp(buf,"z")==0);
-
- // --- note that assert() writes to stdout
- // --- so you should check the "testout.txt" file for assertion failure
- fp=fopen("testout.txt","w");
- assert(fp!=NULL && fp>stdprn);
- memcpy(&tempout,stdout,sizeof(FILE));
- memcpy(stdout,fp,sizeof(FILE));
-
- strcpy(buf,"first line of testout file\n");
- assert(printf(buf)==strlen(buf));
- assert(putchar('a')=='a'); assert(putchar('b')=='b');
- puts("cde");
- assert(ungetc('j',stdout)==EOF); // --- cannot ungetc to an output stream
-
- assert(fclose(stdin)==0); assert(fclose(stdout)==0);
-
- // --- return stdin/stdout to previous settings
- memcpy(stdin,&tempin,sizeof(FILE));
- memcpy(stdout,&tempout,sizeof(FILE));
-
- // --- now we read what we wrote to stdout, to make sure it worked
- fp=fopen("testout.txt","r");
- assert(fp!=NULL && fp>stdprn);
- assert(fgets(buf,100,fp)==buf);
- assert(strcmp(buf,"first line of testout file\n")==0);
- assert(fgets(buf,100,fp)==buf);
- assert(strcmp(buf,"abcde\n")==0);
- assert(fgets(buf,100,fp)==NULL);
- assert(feof(fp)!=0 && ferror(fp)==0);
- assert(fclose(fp)==0);
-
- printf("passed stdio tests...\n");
- printf("==> finished xstdio <==\n");
-
- return;
- }
-